package com.bytedance.danmaku.render.engine.demo.danmu.project

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.Log
import com.bytedance.danmaku.render.engine.control.DanmakuConfig
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.utils.DRAW_TYPE_DIGG_EMPTY_TEXT
import com.bytedance.danmaku.render.engine.demo.utils.DensityUtils.dp2px
import com.bytedance.danmaku.render.engine.demo.utils.dp
import com.bytedance.danmaku.render.engine.render.draw.DrawItem
import com.bytedance.danmaku.render.engine.render.draw.IDrawItemFactory
import com.bytedance.danmaku.render.engine.render.draw.bitmap.BitmapData
import com.bytedance.danmaku.render.engine.render.draw.bitmap.BitmapDrawItem
import kotlin.math.max

/**
 * @Description:可设置文字为空字符图文混排（点赞布局）
 * @Author yanxin
 * @Date:  2025/9/8
 */

class DiggEmptyTextDanmaku : DanmakuData() {

    override var drawType: Int = DRAW_TYPE_DIGG_EMPTY_TEXT
    var diggIcon: BitmapData? = null
    var diggText: EmptyTextDanmaku? = null
    var diggIsLeftIcon: Boolean = true
}

class DiggEmptyTextDrawItem : DrawItem<DiggEmptyTextDanmaku>() {

    override var x: Float = 0F
        set(value) {
            field = value
            updateXY()
        }
    override var y: Float = 0F
        set(value) {
            field = value
            updateXY()
        }

    private val mDiggIconItem: BitmapDrawItem = BitmapDrawItem()
    private val mDiggCountItem: EmptyTextDanmakuDrawItem = EmptyTextDanmakuDrawItem()

    private val mDiggBgPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val mDiggBgRectF = RectF()
    private var mIconPaddingLeft = dp2px(12F)
    private var mCountPaddingRight = 3.dp

    override fun getDrawType(): Int {
        return DRAW_TYPE_DIGG_EMPTY_TEXT
    }

    override fun onBindData(data: DiggEmptyTextDanmaku) {
        data.diggText?.let {
            mDiggCountItem.bindData(it)
        }
        data.diggIcon?.let {
            mDiggIconItem.bindData(it)
        }
    }

    override fun onMeasure(config: DanmakuConfig) {
        mDiggIconItem.measure(config)
        mDiggCountItem.measure(config)
        height = max(mDiggIconItem.height, mDiggCountItem.height)
        width = mDiggIconItem.width + mDiggCountItem.width + mIconPaddingLeft + mCountPaddingRight
    }

    override fun onDraw(canvas: Canvas, config: DanmakuConfig) {
        if (data != null) {
//            drawDiggBg(canvas)
            mDiggCountItem.draw(canvas, config)
            mDiggIconItem.draw(canvas, config)
        }
    }

    override fun recycle() {
        super.recycle()
        mDiggBgPaint.reset()
        mDiggIconItem.recycle()
        mDiggCountItem.recycle()
    }

    private fun drawDiggBg(canvas: Canvas) {
        //todo ：这是边框样式
//        mDiggBgPaint.setStyle(Paint.Style.STROKE); // 设置绘制样式为描边
//        mDiggBgPaint.setStrokeWidth(3f);  // 设置边框宽度
//        mDiggBgPaint.setColor(Color.BLUE);         // 设置边框颜色
//        mDiggBgRectF.set(x, y, x + width, y + height)
//        canvas.drawRoundRect(mDiggBgRectF, height / 2, height / 2, mDiggBgPaint)

        mDiggBgRectF.set(x, y, x + width, y + height)
        mDiggBgPaint.color = Color.BLACK
        mDiggBgPaint.alpha = 66 // 26%
        canvas.drawRoundRect(mDiggBgRectF, height / 2, height / 2, mDiggBgPaint)
    }

    /**
     * Update the x/y of sub-item when the parent's x/y changed
     */
    private fun updateXY() {
        if (data?.diggIsLeftIcon == true) {
            //todo ： 图片在左文字在右
            mDiggIconItem.x = x+mIconPaddingLeft
            mDiggIconItem.y = y + (height - mDiggIconItem.height) / 2
            mDiggCountItem.x = mDiggIconItem.x + mDiggIconItem.width+mCountPaddingRight
            mDiggCountItem.y = y + (height - mDiggCountItem.height) / 2
        } else {
            //todo ： 文字在左图片在右
            mDiggCountItem.x = x+mIconPaddingLeft
            mDiggCountItem.y = y
            mDiggIconItem.x = mDiggCountItem.x + mDiggCountItem.width + mCountPaddingRight
            mDiggIconItem.y = y + (height - mDiggIconItem.height) / 2
        }
    }
}

class DiggEmptyTextDanmakuFactory : IDrawItemFactory {

    override fun getDrawType(): Int {
        return DRAW_TYPE_DIGG_EMPTY_TEXT
    }

    override fun generateDrawItem(): DrawItem<out DanmakuData> {
        return DiggEmptyTextDrawItem()
    }

}